﻿Imports System.Security.Cryptography
Imports System.Text

'Public Class UserSession
'    Public Property Name As String
'    Public Property Key As String
'    Public Property Subscriber As Boolean
'End Class


''' <summary>
''' Generic last.fm exception
''' </summary>
''' <remarks></remarks>
Class LastFMError
    Inherits Exception

    Private _p1 As String

    Sub New()

    End Sub

    Sub New(ByVal p1 As String)
        ' TODO: Complete member initialization 
        _p1 = p1
    End Sub

End Class


Public Class ScrobblerSession
    Public Sub New()
        mCreated = Now
    End Sub

    Public Property Key As String
    Public Property NowPlayingUrl As Uri
    Public Property SubmissionUrl As Uri

    Private mCreated As Date
    Public ReadOnly Property Created As Date
        Get
            Return mCreated
        End Get
    End Property
End Class

Public Class Application
    Public Property ApiKey As String
    Public Property AppSecret As String
End Class

Public Class Song
    Public Enum Sources
        ''' <summary>
        ''' Chosen by the user (the most common value, unless you have a reason for choosing otherwise, use this).
        ''' </summary>
        P
        ''' <summary>
        ''' Non-personalised broadcast (e.g. Shoutcast, BBC Radio 1).
        ''' </summary>
        R
        ''' <summary>
        ''' Personalised recommendation except Last.fm (e.g. Pandora, Launchcast).
        ''' </summary>
        E
        ''' <summary>
        ''' Last.fm (any mode). In this case, the 5-digit Last.fm recommendation key must be appended to this source ID to prove the validity of the submission (for example, "o[0]=L1b48a").
        ''' </summary>
        L
    End Enum
    Public Enum Ratings

        NotApplicable

        ''' <summary>
        ''' Love (on any mode if the user has manually loved the track). This implies a listen
        ''' </summary>
        L
        ''' <summary>
        ''' Ban (only if source=L). This implies a skip, and the client should skip to the next track when a ban happens.
        ''' </summary>
        B
        ''' <summary>
        ''' Skip (only if source=L)
        ''' </summary>
        S
    End Enum
    Public Property Artist As String
    Public Property Track As String
    Public Property Album As String
    Public Property ListenTime As DateTime
    Public Property TrackNumber As Integer
    ' TODO: Music Brainz Id
    Public ReadOnly Property UnixTime As Long
        Get
            Return (ListenTime.ToUniversalTime - New DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds
        End Get
    End Property
    Public Property Duration As Integer
    Public Property Source As Sources
End Class

Public Class Player
    Public Property Name As String
    Public Property Identifier As String
    Public Property Version As String

    Public Shared TestPlayer As Player = New Player With {.Name = "Development Player", .Identifier = "tst", .Version = "1.0"}
End Class




Module Helpers
    Enum HashMethod
        MD5
        SHA1
        SHA384
    End Enum

    Function GenerateHashDigest(ByVal source As String, ByVal algorithm As HashMethod) As String
        Dim hashAlgorithm As HashAlgorithm = Nothing
        Select Case algorithm
            Case HashMethod.MD5
                hashAlgorithm = New MD5CryptoServiceProvider
            Case HashMethod.SHA1
                hashAlgorithm = New SHA1CryptoServiceProvider
            Case HashMethod.SHA384
                hashAlgorithm = New SHA384Managed
            Case Else
                ' Error case.
        End Select

        Dim byteValue = Encoding.UTF8.GetBytes(source)
        Dim hashValue = hashAlgorithm.ComputeHash(byteValue)
        Dim ret As String = ""
        For Each i In hashValue
            Dim m = Conversion.Hex(i).ToLowerInvariant
            If m.Length = 1 Then m = "0" + m
            If m.Length = 0 Then m = "00"
            ret += m
        Next
        Return ret
    End Function

#If DEBUG Then
    Public Sub Message(ByVal s, ByVal k)
        Debug.Print("######################################################")
        Debug.Print(s)
        Debug.Print("------------------------------------------------------")
        Debug.Print(k)
        Debug.Print("######################################################")
        Debug.Print("")
    End Sub
#End If
End Module


